package com.qdxwx.manage.controller.teacher;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.book.BookTeacherService;
import com.qdxwx.data.service.course.CourseTeacherService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.subject.Subject;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.teacher.TeacherQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 讲师Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController extends BaseController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseTeacherService courseTeacherService;
    @Autowired
    private BookTeacherService bookTeacherService;
    @Autowired
    private SubjectService subjectService;

    @Permissions("teacher:list")
    @GetMapping("list.html")
    public String teacher() {
        return "teacher/teacher-list";
    }

    /**
     * 查询讲师列表
     */
    @Permissions("teacher:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Integer starId = TKConvert.toInteger(request.getParameter("starId"), null);
        String searchKey = TKString.trim(request.getParameter("searchKey"));

        TeacherQuery teacher = new TeacherQuery();
        if (searchKey.length() > 0) {
            if (TKString.isMobile(searchKey)) {
                teacher.setMobile(searchKey);
            } else {
                teacher.setName(TKString.queryKey(searchKey));
            }
        }
        if (starId != null) {
            teacher.setStarId(starId);
        }
        List<Teacher> list = teacherService.getList(teacher, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(teacherService.count(teacher));
        return rspData;
    }

    /**
     * 详情
     */
    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        Teacher teacher = teacherService.get(id);
        if (teacher == null) {
            return error404;
        }
        String subjectName = "-";
        if (teacher.getSubjectId() > 0) {
            Subject subject = subjectService.get(teacher.getSubjectId());
            if (subject != null) {
                subjectName = subject.getSubjectName();
            }
        }
        mmap.put("courseCounts", courseTeacherService.countByTeacherId(teacher.getId()));
        mmap.put("bookCounts", bookTeacherService.countByTeacherId(teacher.getId()));
        mmap.put("teacher", teacher);
        mmap.put("subjectName", subjectName);
        return "teacher/teacher-view";
    }

}
